﻿#include "stdafx.h"
#include "Resource.h"
#include "global.h"
#include "controls.h"
#include "Interface.h"
#include "d3dfunc.h"
#include "Filter.h"

int InitControls(HWND hWnd)
{
	HRESULT r = D3DXCreateTextureFromResource(GV.pD3Device, 0, MAKEINTRESOURCE(IDI_ELEMENTS),&GV.Elements);
	r = D3DXCreateTextureFromResource(GV.pD3Device, 0, MAKEINTRESOURCE(IDI_ELEMENTS2),&GV.Elements2);
	r = D3DXCreateTextureFromResource(GV.pD3Device, 0, MAKEINTRESOURCE(IDI_IMGBACK), &GV.ImgBack);

	RECT ElementRect;
	
	SetRect(&ElementRect, 258, 35, 363, 101);
	PictureList *NoiseList = new PictureList(ElementRect, hWnd, 4, 4, OnNoiseList);
	NoiseList->SetItemImage(GV.Elements, PICTLIST_SELECTEDIMAGE, 0, 42, 105, 22);
	NoiseList->AddElement(L"Нет");
	NoiseList->AddElement(L"Гауссовский");
	NoiseList->AddElement(L"Импульсный");
	NoiseList->SetFont(L"Arial", 16, 0xff000000);
	NoiseList->id = 0;
	GV.MyControls = NoiseList;

	SetRect(&ElementRect, 366, 38, 388, 98);
	VSlider *NoiseStr = new VSlider(ElementRect, hWnd, 6, OnNoiseSliderMove, OnNoiseSliderSet);
	NoiseStr->SetImage(GV.Elements, PICTSLDR_BIT, 222, 0, 22, 15);
	NoiseStr->SetImage(GV.Elements, PICTSLDR_PRESSEDBIT, 222, 15, 22, 15);
	NoiseStr->SetImage(GV.Elements, PICTSLDR_BACKGROUND, 200, 0, 22, 60);
	NoiseStr->Flags = NoiseStr->Flags & (ELEMENT_SHOWN ^ 0xffffffff);
	NoiseStr->id = 1;
	InitElement(NoiseStr);

	SetRect(&ElementRect, 391, 35, 465, 57);
	PictureLayer *NoiseValueBack = new PictureLayer(ElementRect, hWnd);
	NoiseValueBack->SetImage(GV.Elements, 105, 42, 74, 22);
	NoiseValueBack->Flags = NoiseValueBack->Flags & (ELEMENT_SHOWN ^ 0xffffffff);
	NoiseValueBack->id = 2;
	InitElement(NoiseValueBack);

	ElementRect.left = 393; ElementRect.right = 463;
	PictureText *NoiseValue = new PictureText(ElementRect, hWnd);
	NoiseValue->SetFont(L"Arial", 16, 0xff000000);
	NoiseValue->SetText(L"12", 0);
	NoiseValue->Flags = NoiseValue->Flags & (ELEMENT_SHOWN ^ 0xffffffff);
	NoiseValue->id = 3;
	InitElement(NoiseValue);



	SetRect(&ElementRect, 475, 35, 580, 101);
	PictureList *FilterList = new PictureList(ElementRect, hWnd, 4, 4, OnFilterList);
	FilterList->SetItemImage(GV.Elements, PICTLIST_SELECTEDIMAGE, 0, 42, 105, 22);
	FilterList->AddElement(L"Усредняющий");
	FilterList->AddElement(L"Пороговый");
	FilterList->AddElement(L"Медианный");
	FilterList->SetFont(L"Arial", 16, 0xff000000);
	FilterList->id = 4;
	InitElement(FilterList);


	SetRect(&ElementRect, 583, 35, 682, 57);
	PictureButton *ImpChar = new PictureButton(ElementRect, hWnd);
	ImpChar->SetImage(GV.Elements2, PICTBTN_IMAGE, 0, 0, 99, 22);
	ImpChar->SetImage(GV.Elements2, PICTBTN_PRESSEDIMAGE, 0, 22, 99, 22);
	ImpChar->OnButtonPressed = OpenCoeff;
	ImpChar->id = 31;
	InitElement(ImpChar);

	SetRect(&ElementRect, 583, 38, 605, 98);
	VSlider *FilterStr = new VSlider(ElementRect, hWnd, 6, OnFilterSliderMove, OnFilterSliderSet);
	FilterStr->SetImage(GV.Elements, PICTSLDR_BIT, 222, 0, 22, 15);
	FilterStr->SetImage(GV.Elements, PICTSLDR_PRESSEDBIT, 222, 15, 22, 15);
	FilterStr->SetImage(GV.Elements, PICTSLDR_BACKGROUND, 200, 0, 22, 60);
	FilterStr->Flags = FilterStr->Flags & (0xffffffff ^ ELEMENT_SHOWN);
	FilterStr->id = 5;
	InitElement(FilterStr);

	SetRect(&ElementRect, 608, 35, 682, 57); 
	PictureLayer *FilterSigmaBack = new PictureLayer(ElementRect, hWnd);
	FilterSigmaBack->SetImage(GV.Elements, 105, 42, 74, 22);
	FilterSigmaBack->id = 6;
	FilterSigmaBack->Flags = FilterSigmaBack->Flags & (0xffffffff ^ ELEMENT_SHOWN);
	InitElement(FilterSigmaBack);

	ElementRect.left = 610; ElementRect.right = 681;
	PictureText *FilterSigma = new PictureText(ElementRect, hWnd);
	FilterSigma->SetFont(L"Arial", 16, 0xff000000);
	FilterSigma->SetText(L"", 0);
	FilterSigma->id = 7;
	FilterSigma->Flags = FilterSigma->Flags & (0xffffffff ^ ELEMENT_SHOWN);
	InitElement(FilterSigma);

	SetRect(&ElementRect, 695, 35, 871, 59);
	PictureButton *DiffImage1 = new PictureButton(ElementRect, hWnd);
	DiffImage1->OnButtonButtonDown = OnDiff1ButtonPressed;
	DiffImage1->OnLooseFocus    = OnDiff1LooseFocus;
	DiffImage1->SetImage(GV.Elements, PICTBTN_IMAGE, 0, 86, 176, 24);
	DiffImage1->SetImage(GV.Elements, PICTBTN_PRESSEDIMAGE, 0, 110, 176, 24);
	DiffImage1->KeepPressed = 1;
	DiffImage1->id = 8;
	InitElement(DiffImage1);

	SetRect(&ElementRect, 697, 35, 869, 59);
	PictureText *DiffImage1Text = new PictureText(ElementRect, hWnd);
	DiffImage1Text->SetFont(L"Arial", 16, 0xff000000);
	DiffImage1Text->SetText(L"Исходное", 0);
	DiffImage1Text->id = 9;
	DiffImage1Text->Flags = DiffImage1Text->Flags | ELEMENT_GHOST;
	DiffImage1->Next = DiffImage1Text;

	SetRect(&ElementRect, 695, 71, 871, 95);
	PictureButton *DiffImage2 = new PictureButton(ElementRect, hWnd);
	DiffImage2->OnButtonButtonDown = OnDiff2ButtonPressed;
	DiffImage2->OnLooseFocus    = OnDiff2LooseFocus;
	DiffImage2->SetImage(GV.Elements, PICTBTN_IMAGE, 0, 86, 176, 24);
	DiffImage2->SetImage(GV.Elements, PICTBTN_PRESSEDIMAGE, 0, 110, 176, 24);
	DiffImage2->KeepPressed = 1;
	DiffImage2->id = 10;
	DiffImage1Text->Next = DiffImage2;

	SetRect(&ElementRect, 697, 71, 869, 95);
	PictureText *DiffImage2Text = new PictureText(ElementRect, hWnd);
	DiffImage2Text->SetFont(L"Arial", 16, 0xff000000);
	DiffImage2Text->SetText(L"Фильтрованное", 0);
	DiffImage2Text->id = 11;
	DiffImage2Text->Flags = DiffImage1Text->Flags | ELEMENT_GHOST;
	DiffImage2->Next = DiffImage2Text;

	SetRect(&ElementRect, 41, 109, 217, 253);
	PictureDraggableImage *DragSrc = new PictureDraggableImage(ElementRect, hWnd,  0);
	DragSrc->background.Image = GV.ImgBack;
	SetRect(&(DragSrc->background.Coords), 0, 0, 176, 144);
	DragSrc->id = 12;
	DiffImage2Text->Next = DragSrc;

	OffsetRect(&ElementRect, 217, 0);
	PictureDraggableImage *DragNos = new PictureDraggableImage(ElementRect, hWnd, 0);
	DragNos->background.Image = GV.ImgBack;
	SetRect(&(DragNos->background.Coords), 0, 0, 176, 144);
	DragNos->id = 13;
	InitElement(DragNos);

	OffsetRect(&ElementRect, 217, 0);
	PictureDraggableImage *DragFlt = new PictureDraggableImage(ElementRect, hWnd, 0);
	DragFlt->background.Image = GV.ImgBack;
	SetRect(&(DragFlt->background.Coords), 0, 0, 176, 144);
	DragFlt->id = 14;
	DragNos->Next = DragFlt;

	OffsetRect(&ElementRect, 217, 0);
	PictureDraggableImage *DragDif = new PictureDraggableImage(ElementRect, hWnd, 0);
	DragDif->background.Image = GV.ImgBack;
	SetRect(&(DragDif->background.Coords), 0, 0, 176, 144);
	DragDif->id = 15;
	DragFlt->Next = DragDif;

	SetRect(&ElementRect, 877, 109, 945, 131);
	PictureText *PSNRLabel = new PictureText(ElementRect, hWnd);
	PSNRLabel->SetFont(L"Arial", 16, 0xff000000);
	PSNRLabel->SetText(L"PSNR=", 0);
	PSNRLabel->id = 34;
	InitElement(PSNRLabel);

	OffsetRect(&ElementRect, 0, 22);
	PictureText *PSNRVal = new PictureText(ElementRect, hWnd);
	PSNRVal->SetFont(L"Arial", 16, 0xff000000);
	PSNRVal->SetText(L"0.00дБ", 0);
	PSNRVal->id = 35;
	InitElement(PSNRVal);

	SetRect(&ElementRect, 65, 271, 115, 292);
	PictureButton *FullSize = new PictureButton(ElementRect, hWnd);
	FullSize->OnButtonPressed = OnFullImageButton;
	FullSize->SetImage(GV.Elements, PICTBTN_IMAGE, 150, 0, 50, 21);
	FullSize->SetImage(GV.Elements, PICTBTN_PRESSEDIMAGE, 150, 21, 50, 21);
	FullSize->id = 16;
	InitElement(FullSize);

	OffsetRect(&ElementRect, 0, 42);
	PictureButton *OnetoOne = new PictureButton(ElementRect, hWnd);
	OnetoOne->OnButtonPressed = OnOnetoOneImageButton;
	OnetoOne->SetImage(GV.Elements, PICTBTN_IMAGE, 0, 0, 50, 21);
	OnetoOne->SetImage(GV.Elements, PICTBTN_PRESSEDIMAGE, 0, 21, 50, 21);
	OnetoOne->id = 17;
	FullSize->Next = OnetoOne;

	OffsetRect(&ElementRect, 0, 42);
	PictureButton *TwotoOne = new PictureButton(ElementRect, hWnd);
	TwotoOne->OnButtonPressed = OnTwotoOneImageButton;
	TwotoOne->SetImage(GV.Elements, PICTBTN_IMAGE, 50, 0, 50, 21);
	TwotoOne->SetImage(GV.Elements, PICTBTN_PRESSEDIMAGE, 50, 21, 50, 21);
	TwotoOne->id = 18;
	OnetoOne->Next = TwotoOne;

	OffsetRect(&ElementRect, 0, 42);
	PictureButton *FourtoOne = new PictureButton(ElementRect, hWnd);
	FourtoOne->OnButtonPressed = OnFourtoOneImageButton;
	FourtoOne->SetImage(GV.Elements, PICTBTN_IMAGE, 100, 0, 50, 21);
	FourtoOne->SetImage(GV.Elements, PICTBTN_PRESSEDIMAGE, 100, 21, 50, 21);
	FourtoOne->id = 19;
	TwotoOne->Next = FourtoOne;
	
	SetRect(&ElementRect, 130, 271, 482, 559);
	PictureImage *BigImage1 = new PictureImage(ElementRect, hWnd, 0, 0);
	BigImage1->background.Image = GV.ImgBack;
	SetRect(&(BigImage1->background.Coords), 0, 0, 255, 255);
	BigImage1->id = 20;
	FourtoOne->Next = BigImage1;

	OffsetRect(&ElementRect, 403, 0);
	PictureImage *BigImage2 = new PictureImage(ElementRect, hWnd, 0, 1);
	BigImage2->background.Image = GV.ImgBack;
	SetRect(&(BigImage2->background.Coords), 0, 0, 255, 255);
	BigImage2->SetLink(BigImage1);
	BigImage2->id = 21;
	BigImage1->Next = BigImage2;

	SetRect(&ElementRect, 687, 60, 905, 151);
	PictureLayer* DiffImg1LstB = new PictureLayer(ElementRect, hWnd);
	DiffImg1LstB->Flags = DiffImg1LstB->Flags & (0xffffffff ^ ELEMENT_SHOWN);
	DiffImg1LstB->SetImage(GV.Elements, 0, 162, 218, 91);
	DiffImg1LstB->id = 22;
	BigImage2->Next = DiffImg1LstB;

	SetRect(&ElementRect, 690, 61, 902, 145);
	PictureList * DiffImg1Lst = new PictureList(ElementRect, hWnd, 3, 3, OnDiffListSetItem);
	DiffImg1Lst->Flags = DiffImg1Lst->Flags & (0xffffffff ^ ELEMENT_SHOWN);
	DiffImg1Lst->SetItemImage(GV.Elements, PICTLIST_SELECTEDIMAGE, 0, 134, 209, 28);
	DiffImg1Lst->AddElement(L"Исходное");
	DiffImg1Lst->AddElement(L"Зашумленное");
	DiffImg1Lst->AddElement(L"Фильтрованное");
	DiffImg1Lst->SetFont(L"Arial", 16, 0xff000000);
	DiffImg1Lst->id = 23;
	DiffImg1LstB->Next = DiffImg1Lst;
	
	SetRect(&ElementRect, 687, 96, 905, 187);
	PictureLayer* DiffImg2LstB = new PictureLayer(ElementRect, hWnd);
	DiffImg2LstB->Flags = DiffImg2LstB->Flags & (0xffffffff ^ ELEMENT_SHOWN);
	DiffImg2LstB->SetImage(GV.Elements, 0, 162, 218, 91);
	DiffImg2LstB->id = 24;
	DiffImg1Lst->Next = DiffImg2LstB;

	SetRect(&ElementRect, 690, 97, 902, 181);
	PictureList * DiffImg2Lst = new PictureList(ElementRect, hWnd, 3, 3, OnDiffListSetItem);
	DiffImg2Lst->Flags = DiffImg2Lst->Flags & (0xffffffff ^ ELEMENT_SHOWN);
	DiffImg2Lst->SetItemImage(GV.Elements, PICTLIST_SELECTEDIMAGE, 0, 134, 209, 28);
	DiffImg2Lst->AddElement(L"Исходное");
	DiffImg2Lst->AddElement(L"Зашумленное");
	DiffImg2Lst->AddElement(L"Фильтрованное");
	DiffImg2Lst->SelectedItem = 2;
	DiffImg2Lst->SetFont(L"Arial", 16, 0xff000000);
	DiffImg2Lst->id = 25;
	DiffImg2LstB->Next = DiffImg2Lst;

	SetRect(&ElementRect, 0, 579, 950, 600);
	PictureLayer * StatusBar = new PictureLayer(ElementRect, hWnd);
	StatusBar->Flags = StatusBar->Flags | PICTLAYER_COPY;
	StatusBar->SetImage(GV.Elements, 106, 42, 52, 21);
	StatusBar->id = 27;
	DiffImg2Lst->Next = StatusBar;

	SetRect(&ElementRect, 2, 580, 950, 599);
	PictureText *ImageProp = new PictureText(ElementRect, hWnd);
	ImageProp->SetFont(L"Arial", 16, 0xff000000);
	ImageProp->SetText(L"", 0);
	ImageProp->id = 26;
	InitElement(ImageProp);

	SetRect(&ElementRect, 41, 5, 217, 30);
	PictureText *SourceLabel = new PictureText(ElementRect, hWnd);
	SourceLabel->SetFont(L"Arial", 18, 0xff000000);
	SourceLabel->SetText(L"Исходное:", 0);
	SourceLabel->id = 28;
	InitElement(SourceLabel);

	OffsetRect(&ElementRect, 217, 0);
	PictureText *NoiseLabel = new PictureText(ElementRect, hWnd);
	NoiseLabel->SetFont(L"Arial", 18, 0xff000000);
	NoiseLabel->SetText(L"Зашумленное:", 0);
	NoiseLabel->id = 29;
	InitElement(NoiseLabel);

	OffsetRect(&ElementRect, 217, 0);
	PictureText *FilterLabel = new PictureText(ElementRect, hWnd);
	FilterLabel->SetFont(L"Arial", 18, 0xff000000);
	FilterLabel->SetText(L"Фильтрованное:", 0);
	FilterLabel->id = 30;
	InitElement(FilterLabel);

	OffsetRect(&ElementRect, 217, 0);
	PictureText *DiffLabel = new PictureText(ElementRect, hWnd);
	DiffLabel->SetFont(L"Arial", 18, 0xff000000);
	DiffLabel->SetText(L"Разность:", 0);
	DiffLabel->id = 33;
	InitElement(DiffLabel);
	// Next id = 36
	// + 32
	return 0;
}

int OnNoiseList(PictureList* This)
{
	PictureControl *slNoise = GetControlByID(1);
	PictureControl *lNoise = GetControlByID(2);
	PictureControl *txtNoise = GetControlByID(3);
	if (This->SelectedItem == 0)
	{
		slNoise->Flags  =  slNoise->Flags & (ELEMENT_SHOWN ^ 0xffffffff);
		lNoise->Flags   =  lNoise->Flags  & (ELEMENT_SHOWN ^ 0xffffffff);
		txtNoise->Flags = txtNoise->Flags & (ELEMENT_SHOWN ^ 0xffffffff);
	}
	else
	{
		slNoise->Flags  =  slNoise->Flags | ELEMENT_SHOWN;
		lNoise->Flags   =  lNoise->Flags  | ELEMENT_SHOWN;
		txtNoise->Flags = txtNoise->Flags | ELEMENT_SHOWN;
	}
	OnNoiseSliderMove((VSlider *)slNoise);
	GV.LoadingPhase = 102;
	return 1;
}

int OnNoiseSliderMove(VSlider* This)
{
	WCHAR Value[16];
	PictureList *Type = (PictureList *) GetControlByID(0);
	PictureText *Text = (PictureText *) GetControlByID(3);
	if (Type->SelectedItem == 1)
	{
		DWORD Sigma = (6 - This->Position) * 3;
		WCHAR Value[16];
		Value[0] = L'σ';
		Value[1] = L'=';
		GetValueString(Value, 2, Sigma, -4, 0);
		Text->SetText(Value, 0);
	}
	else if (Type->SelectedItem == 2)
	{
		DWORD Persent = (6 - This->Position) * 2;
		DWORD Len;
		WCHAR Value[16];
		Value[0] = 'p'; Value[1] = '=';
		GetValueString(Value, 2, Persent, 0, &Len);
		Value[2 + Len] = '%';
		Value[3 + Len] = 0;
		Text->SetText(Value, 0);
	}
	return 1;
}
int OnNoiseSliderSet(VSlider* This)
{
	GV.LoadingPhase = 102;
	return 1;
}

int OnFilterList(PictureList* This)
{
	PictureLayer *Back = (PictureLayer *) GetControlByID(6);
	PictureText *Text =  (PictureText *)  GetControlByID(7);
	VSlider *Slider = (VSlider *) GetControlByID(5);
	PictureControl *Coeffs = GetControlByID(31);
	if (This->SelectedItem == 0)
	{
		Slider->Flags = Slider->Flags & (0xffffffff ^ ELEMENT_SHOWN);
		Back->Flags =Back->Flags & (0xffffffff ^ ELEMENT_SHOWN);
		Text->Flags = Text->Flags & (0xffffffff ^ ELEMENT_SHOWN);
		Coeffs->Flags = Coeffs->Flags | ELEMENT_SHOWN;
	}
	else
	{
		Slider->Flags = Slider->Flags | ELEMENT_SHOWN;
		Back->Flags = Back->Flags | ELEMENT_SHOWN;
		Text->Flags = Text->Flags | ELEMENT_SHOWN;
		Coeffs->Flags = Coeffs->Flags & (0xffffffff ^ ELEMENT_SHOWN);
	}
	if (This->SelectedItem == 2)
	{
		Slider->SetImage(GV.Elements, PICTSLDR_BACKGROUND, 200, 60, 22, 60);
		Slider->MaxValue = 3;
		if (Slider->Position > 2) Slider->Position = 2;
	}
	else
	{
		Slider->SetImage(GV.Elements, PICTSLDR_BACKGROUND, 200, 0, 22, 60);
		Slider->MaxValue = 6;
	}
	OnFilterSliderMove(Slider);
	GV.LoadingPhase = 103;
	return 1;
}

int OnFilterSliderMove(VSlider* This)
{
	PictureList *FilterList = (PictureList *) GetControlByID(4);
	if (FilterList->SelectedItem == 1)
	{
		DWORD Step = (6 - This->Position) * 10;
		WCHAR Value[16];
		DWORD Len;
		Value[0] = L'M'; Value[1] = L'i'; Value[2] = L'n'; Value[3] = L'=';
		GetValueString(Value, 4, Step * 100 / 255, 0, &Len);
		Value[4 + Len] = '%'; Value[5 + Len] = 0;
		((PictureText *)GetControlByID(7))->SetText(Value, 0);
	}
	else if (FilterList->SelectedItem == 2)
	{
		DWORD Size = (3 - This->Position);
		WCHAR Value[16];
		GetWindowString(Size, Value);
		((PictureText *)GetControlByID(7))->SetText(Value, 0);
	}
	return 1;
}
int OnFilterSliderSet(VSlider* This)
{
	GV.LoadingPhase = 103;
	return 1;
}

int OnDiff1ButtonPressed(PictureButton *This)
{
	if (GV.ActiveItem == This->id)
	{
		GV.PressedItem = -1;
		This->OnLooseFocus(This);
		return 1;
	}
	PictureControl *Back = GetControlByID(22);
	PictureControl *Lst  = GetControlByID(23);
	Back->Flags = Back->Flags | ELEMENT_SHOWN;
	Lst->Flags  = Lst->Flags  | ELEMENT_SHOWN;
	return 1;
}

int OnDiff1LooseFocus(PictureControl *This)
{
	PictureControl *Back = GetControlByID(22);
	PictureList *Lst  = (PictureList *) GetControlByID(23);
	PictureText	   *Text = (PictureText *)GetControlByID(9);
	if (Lst->SelectedItem == 0)
		Text->SetText(L"Исходное", 0);
	else if (Lst->SelectedItem == 1)
		Text->SetText(L"Зашумленное", 0);
	else
		Text->SetText(L"Фильтрованное", 0);
	Back->Flags = Back->Flags & (0xffffffff ^ ELEMENT_SHOWN);
	Lst->Flags  = Lst->Flags  & (0xffffffff ^ ELEMENT_SHOWN);
	((PictureButton *)This)->UnPress(0,0);
	return 1;
}

int OnDiff2ButtonPressed(PictureButton *This)
{
	if (GV.ActiveItem == This->id)
	{
		GV.PressedItem = -1;
		This->OnLooseFocus(This);
		return 1;
	}
	PictureControl *Back = GetControlByID(24);
	PictureControl *Lst  = GetControlByID(25);
	Back->Flags = Back->Flags | ELEMENT_SHOWN;
	Lst->Flags  = Lst->Flags  | ELEMENT_SHOWN;
	return 1;
}

int OnDiff2LooseFocus(PictureControl *This)
{
	PictureControl *Back = GetControlByID(24);
	PictureList *Lst  = (PictureList *) GetControlByID(25);
	PictureText	   *Text = (PictureText *)GetControlByID(11);
	if (Lst->SelectedItem == 0)
		Text->SetText(L"Исходное", 0);
	else if (Lst->SelectedItem == 1)
		Text->SetText(L"Зашумленное", 0);
	else
		Text->SetText(L"Фильтрованное", 0);
	Back->Flags = Back->Flags & (0xffffffff ^ ELEMENT_SHOWN);
	Lst->Flags  = Lst->Flags  & (0xffffffff ^ ELEMENT_SHOWN);
	((PictureButton *)This)->UnPress(0,0);
	return 1;
}

int OnDiffListSetItem(PictureList *This)
{
	GV.LoadingPhase = 104;
	return 1;
}

int OnFullImageButton(PictureButton *This)
{
	PictureImage *Img = (PictureImage*) GetControlByID(20);
	Img->ChangeMultiplyer(0);
	return 1;
}
int OnOnetoOneImageButton(PictureButton *This)
{
	PictureImage *Img = (PictureImage*) GetControlByID(20);
	Img->ChangeMultiplyer(1);
	return 1;
}
int OnTwotoOneImageButton(PictureButton *This)
{
	PictureImage *Img = (PictureImage*) GetControlByID(20);
	Img->ChangeMultiplyer(2);
	return 1;
}
int OnFourtoOneImageButton(PictureButton *This)
{
	PictureImage *Img = (PictureImage*) GetControlByID(20);
	Img->ChangeMultiplyer(4);
	return 1;
}

int OpenTestImage(HWND hWnd)
{
	OPENFILENAME oFileName;
	ZeroMemory(&oFileName, sizeof(oFileName));
	oFileName.lStructSize = OPENFILENAME_SIZE_VERSION_400;
	oFileName.hwndOwner = hWnd;
	oFileName.lpstrFilter = L"Image files\0*.JPG;*.jpg;*.PNG;*.png\0";
	oFileName.lpstrFile = GV.FileName;
	oFileName.lpstrFile[0] = 0;
	// Size of filename in TCHARS
	oFileName.nMaxFile = 1024;
	oFileName.lpstrInitialDir = GV.Directory;
	oFileName.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST;
	BOOL r = GetOpenFileName(&oFileName);
	if (!r) return 0;
	// Saving file directory for future Save/Load
	WORD i = 0;
	GV.FileNameOffset = oFileName.nFileOffset;
	GV.FileExtOffset = oFileName.nFileExtension;
	while (i < oFileName.nFileOffset)
	{
		GV.Directory[i] = GV.FileName[i];
		i++;
	}
	GV.Directory[i] = 0;
	return 1;
}

int OpenCoeff(PictureButton *This)
{
	DialogBox(0, MAKEINTRESOURCE(ID_DLGBOX), 0, MyDlgProc);
	return 1;
}